<?php

require_once './baiduTop.php';

$cardsFilePath = 'redian3.txt';

$mtime = 0;
if (file_exists($cardsFilePath)) {
  $mtime = filemtime($cardsFilePath);
}
$cards = null;
if (date("Y-m-d", $mtime) === date("Y-m-d")) {
  $cards = json_decode(file_get_contents($cardsFilePath), true);
} else {
  $cards = BaiduTop::getCards();
  file_put_contents($cardsFilePath, json_encode($cards));
}

if (count($cards) > 0) {
  $content = $cards[0]['content'];

  $html = file_get_contents('bangdan.htm');

  for ($i = 0; $i < count($content) && $i < 10; $i += 1) {
    $jkm = $i;
    $url = 'http://www.baikecount.com/tz.php?word=' . rawurlencode($content[$i]['word']) . '&jkm=' . $jkm;
    $html = str_replace('_anters_' . $i . '_zs', '', $html);
    $html = str_replace('_anters_' . $i . '_lj', $url, $html);
    $html = str_replace('_anters_' . $i, $content[$i]['word'], $html);
  }
  $html = str_replace('zlsbtsfzddf', json_encode($content), $html);

  echo $html;
}
